package cn.rhzz.restore;

import java.io.*;
import java.util.*;
import org.apache.commons.io.FileUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

public class StatusFile {

    private static final Logger logger = LoggerFactory.getLogger(StatusFile.class);
    
	public static String getStatusFile(String filepath) throws IOException {

		if (filepath == null || "".equals(filepath.trim())) {
			logger.error("Error: cannot find the status file, please give the status file path as paramter getStatusFile()");
			throw new IOException("Error: cannot find the status file, please give the status file path as paramter");
		}

		File file = new File(filepath);

		if (!file.exists()) {
			logger.warn("Error: getStatusFile() status file doesn't exist in path: " + filepath);
			return "";
		}

		logger.info("Status file: " + file.getAbsolutePath());
		String content = null;
		try {
			content = FileUtils.readFileToString(file, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}

		return content;

	}

	public static void setStatusFile(String filepath, String content) throws IOException {

		if (filepath == null || "".equals(filepath.trim())) {
			logger.error("Error: cannot find the status file, please give the status file path as paramter setStatusFile()");
			throw new IOException("Error: cannot find the status file, please give the status file path as paramter");
		}

		File file = new File(filepath);

		if (!file.exists()) {
			logger.warn("Error: setStatusFile() status file doesn't exist in path: " + filepath);
		}

		System.out.println("Status file: " + file.getAbsolutePath());
		try {
			FileUtils.writeStringToFile(file, content, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}